﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1.Templates
{
    public class DapperDALTemplate : TemplatesBase
    {
        public DapperDALTemplate() : base("Dal")
        {

        }

        /// <summary>
        /// 命名空间
        /// </summary>
        public override string namespacestr
        {
            get
            {
                return Entrance.attrnamespacestr + ".DAL.Impl";
            }
        }

        public override void SaveFile(List<EntityAttrList> list)
        {
            if (list != null && list.Count > 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                //表描述
                string txtTableDesc = list.FirstOrDefault().TableDescription;
                string txtTableName = list.FirstOrDefault().TableName;
                stringBuilder.AppendLine("using DapperDal.DapperBase;");
                stringBuilder.AppendLine("using " + Entrance.attrnamespacestr + ".Entity;");
                stringBuilder.AppendLine("using System;");
                stringBuilder.AppendLine("using System.Collections.Generic;");
                stringBuilder.AppendLine("using System.Linq;");
                stringBuilder.AppendLine("");
                stringBuilder.AppendLine("namespace " + namespacestr);
                stringBuilder.AppendLine("{");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("/// <summary>");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("/// " + txtTableDesc + "数据层");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("/// <summary>");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("public partial class " + txtTableName + FileExt + " : DapperBaseDAL<" + GetPrimaryKey(list) + "," + txtTableName + "Entity>, I" + txtTableName + "Dal");
                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("{");


                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("#region 构造函数");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("public " + txtTableName + FileExt + "() : base(\"" + Entrance.attrdbTableName + "\")");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("{");
                stringBuilder.AppendLine("");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("}");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("#endregion");

                stringBuilder.AppendLine("");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("public override bool SoftDelete(" + GetPrimaryKey(list) + " id)");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("{");
                stringBuilder.Append(new string(' ', 12));
                stringBuilder.AppendLine("return false;");
                stringBuilder.Append(new string(' ', 8));
                stringBuilder.AppendLine("}");


                stringBuilder.Append(new string(' ', 4));
                stringBuilder.AppendLine("}");
                stringBuilder.AppendLine("}");

                CreateFile(txtTableName, stringBuilder.ToString());
            }
        }
    }
}
